<template>
  <div id="commentList">
    <h1 class="con-right-title">
      <i class="fa fa-reply" @click="cancel"></i>
      {{ title }}/评论列表
    </h1>

    <!--推荐位列表-->
    <el-card class="box-card">
      <div class="table-conBtn">
        <freshCom @searchList="searchList"></freshCom>
      </div>

      <el-table v-loading="loading" :data="tableData" stripe style="width: 100%" max-height="490" ref="table">
        <el-table-column type="index" width="60" align="center" label="序号">
          <template slot-scope="{ $index }">
            {{ $index | filterPage(searchParams.pageNum, searchParams.pageSize) }}
          </template>
        </el-table-column>

        <el-table-column label="评论人头像" min-width="100">
          <template slot-scope="{ row }">
            <div class="avatar-box">
              <el-image fit="contain" :src="row.user ? row.user.avatar : ''"></el-image>
            </div>
          </template>
        </el-table-column>

        <el-table-column
          label="评论人昵称"
          prop="user.nickName"
          min-width="100"
        ></el-table-column>

        <el-table-column
          label="大鹏号"
          prop="user.dpAccount"
          min-width="100"
        ></el-table-column>

        <el-table-column label="评论内容" width="260">
          <template slot-scope="{ row }">
            <span v-html="filterContent(row)"></span>
          </template>
        </el-table-column>

        <el-table-column label="视图预览" prop="images" min-width="160">
          <template slot-scope="{ row }">
            <span v-if="row.images && row.images.length > 0">
              <listImgShow :imgInfo="row.images"></listImgShow>
            </span>
            <span v-else>-</span>
          </template>
        </el-table-column>

        <el-table-column
          label="点赞数"
          prop="likeCount"
          min-width="160"
        ></el-table-column>

        <el-table-column label="是否置顶" prop="isRecommend" min-width="160">
          <template slot-scope="{ row }">
            {{ row.isRecommend ? '是' : '否' }}
          </template>
        </el-table-column>

        <el-table-column
          label="评论时间"
          prop="createdDate"
          min-width="160"
        ></el-table-column>

        <el-table-column
          label="回复总条数"
          prop="replyCount"
          min-width="100"
          align="center"
        ></el-table-column>

        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="{ row }">
            <template v-permission="`wh:system:circle:commentTop`">
              <el-button v-if="row.isRecommend" type="text" @click="cancelRecommend(row.id)">
                取消置顶
              </el-button>

              <el-button v-else type="text" @click="toRecommend(row.id)">
                置顶
              </el-button>
            </template>

            <el-button v-permission="`wh:system:circle:commentAddLike`" type="text" @click="showAddPraise(row.id, row.likeCount)">
              加赞
            </el-button>

            <el-button type="text" @click="addComment(row.id)">
              添加回复
            </el-button>

            <el-button v-if="row.replyCount > 0" type="text" @click=" changeView( isMyPub ? 'wh:circleMCBack:list' : 'wh:circleCBack:list', row.id)">
              回复列表
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页-->
      <div class="page">
        <el-pagination v-if="total > 0" background :layout=" total <= searchParams.pageSize ? 'total' : 'total, prev, pager, next, jumper'" :total="total" :page-size="searchParams.pageSize" :current-page="searchParams.pageNum"></el-pagination>
      </div>
    </el-card>

    <!--常规位列表-->
    <el-card class="box-card">
      <div class="table-conBtn">
        <freshCom @searchList="searchBaseList"></freshCom>
      </div>

      <el-table v-loading="loading2" :data="tableData2" stripe style="width: 100%" max-height="490" ref="table2">
        <el-table-column type="index" width="60" align="center" label="序号">
          <template slot-scope="{ $index }">
            {{ $index | filterPage(searchParams2.pageNum, searchParams2.pageSize) }}
          </template>
        </el-table-column>

        <el-table-column label="评论人头像" min-width="100">
          <template slot-scope="{ row }">
            <div class="avatar-box">
              <el-image fit="contain" :src="row.user ? row.user.avatar : ''"></el-image>
            </div>
          </template>
        </el-table-column>

        <el-table-column
          label="评论人昵称"
          prop="user.nickName"
          min-width="100"
        ></el-table-column>

        <el-table-column
          label="大鹏号"
          prop="user.dpAccount"
          min-width="100"
        ></el-table-column>

        <el-table-column label="评论内容" width="260">
          <template slot-scope="{ row }">
            <span v-html="filterContent(row)"></span>
          </template>
        </el-table-column>

        <el-table-column label="视图预览" width="160" align="center">
          <template slot-scope="{ row }">
            <span v-if="row.images && row.images.length > 0">
              <listImgShow :imgInfo="row.images"></listImgShow>
            </span>
            <span v-else>-</span>
          </template>
        </el-table-column>

        <el-table-column
          label="点赞数"
          prop="likeCount"
          min-width="160"
        ></el-table-column>

        <el-table-column label="是否置顶" prop="isRecommend" min-width="160">
          <template slot-scope="{ row }">
            {{ row.isRecommend ? '是' : '否' }}
          </template>
        </el-table-column>

        <el-table-column
          label="评论时间"
          prop="createdDate"
          min-width="160"
        ></el-table-column>

        <el-table-column
          label="回复总条数"
          prop="replyCount"
          min-width="100"
          align="center"
        ></el-table-column>

        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="{ row }">
            <template v-permission="`wh:system:circle:commentTop`">
              <el-button v-if="row.isRecommend" type="text" @click="cancelRecommend(row.id)">
                取消置顶
              </el-button>

              <el-button v-else type="text" @click="toRecommend(row.id)">
                置顶
              </el-button>
            </template>

            <el-button v-permission="`wh:system:circle:commentAddLike`" type="text" @click="showAddPraise(row.id, row.likeCount)">
              加赞
            </el-button>

            <el-button type="text" @click="addComment(row.id)">
              添加回复
            </el-button>

            <el-button v-if="row.replyCount > 0" type="text" @click="changeView( isMyPub ? 'wh:circleMCBack:list' : 'wh:circleCBack:list', row.id)">
              回复列表
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页-全局组件-->
      <pageCom
        :searchParams="searchParams2"
        :total="total2"
        @searchList="searchBaseList"
      ></pageCom>
    </el-card>

    <!--加赞弹窗-->
    <dialogAddPraise
      v-if="addPraiseData.isShow"
      :addPraiseData="addPraiseData"
      @refresh="refresh"
    ></dialogAddPraise>

    <!--回复弹窗-->
    <dialogReply
      v-if="addCommentData.isShow"
      :dialogData="addCommentData"
      @refresh="refresh"
    ></dialogReply>
  </div>
</template>

<script>
import dialogReply from '@common/dialogReply'
import { filterCon } from '@utils/pubFunction'
import dialogAddPraise from '@component/common/dialogAddPraise'
import { getBase, getRecommend, recommend, cRecommend } from '@/api/common/commentsAPI'
export default {
  name: 'CommentList',
  msg: '圈子管理-圈子维护/我的发布-评论列表',
  components: {
    dialogAddPraise,
    dialogReply
  },
  data() {
    return {
      id: null,
      topicType: 'CIRCLE',
      loading: false,
      loading2: false,
      title: '', // 作品维护/我的发布
      isMyPub: false, // 是否我的发布
      from: '',
      powerAry: [],
      total: 0,
      total2: 0,
      tableData: [], // 常规位table内容
      tableData2: [], // 推荐位table内容
      searchParams: {
        pageNum: 1, // 当前页数
        pageSize: 10 // 页面显示条数
      },
      searchParams2: {
        pageNum: 1, // 当前页数
        pageSize: 10 // 页面显示条数
      },
      addPraiseData: {
        isShow: false,
        likeCount: '',
        commentId: ''
      }, //加赞弹窗
      addCommentData: {
        isShow: false,
        commentId: ''
      } //添加回复弹窗
    }
  },
  computed: {
    lookList() {
      return this.$store.getters.lookList
    }
  },
  watch: {
    // $route.path改变时，mounted()不执行,通过监听$route.path，执行进入页面方法
    '$route.path': 'init'
  },
  // 路由离开时清空用户信息
  beforeRouteLeave(to, from, next) {
    if (to) {
      this.$destroy()
    }
    next()
  },
  mounted() {
    this.init()
  },
  methods: {
    /* 进入页面执行的方法 */
    init() {
      this.from = this.$route.query.from
      this.id = this.$route.query.id
      if (this.from === 'circle') {
        this.title = '圈子维护'
      }
      this.searchParams.pageNum = 1
      this.searchParams2.pageNum = 1
      this.searchList()
      this.searchBaseList()
    },
    /* 获取列表 - 推荐位*/
    async searchList() {
      this.loading = true
      let obj = {
        topicType: this.topicType,
        topicId: this.id
      }
      let response = await getRecommend(obj)
      this.loading = false
      this.tableData = response.rows
      this.total = response.total
    },
    /* 获取列表-常规位  */
    async searchBaseList() {
      this.loading2 = true
      let obj = {
        topicType: this.topicType,
        topicId: this.id,
        pageSize: this.searchParams2.pageSize,
        pageNum: this.searchParams2.pageNum
      }
      let response = await getBase(obj)
      this.loading2 = false
      this.tableData2 = response.rows
      this.total2 = response.total
    },
    /* 过滤返回评论内容 */
    filterContent(item) {
      return filterCon(item)
    },
    /* 取消置顶 */
    async cancelRecommend(commentId) {
      this.$confirm('确认要取消该置顶吗?', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let isSuccess = await cRecommend(commentId)
        if (isSuccess) {
          this.refresh()
        }
      })
    },
    /* 置顶 */
    async toRecommend(commentId) {
      this.$confirm('确认要置顶该评论吗?', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let isSuccess = await recommend(commentId)
        if (isSuccess) {
          this.refresh()
        }
      })
    },
    /* 加赞 */
    showAddPraise(commentId, likeCount) {
      this.addPraiseData.isShow = true
      this.addPraiseData.commentId = commentId
      this.addPraiseData.likeCount = likeCount
    },
    /* 刷新列表 */
    refresh() {
      this.searchList()
      this.searchBaseList()
    },
    /* 添加回复 */
    addComment(commentId) {
      this.addCommentData.isShow = true
      this.addCommentData.commentId = commentId
    },
    /* 换页 */
    changePage2(page) {
      this.searchParams2.pageNum = page
      this.searchBaseList()
    },
    /* 后退 */
    cancel() {
      this.$router.go(-1)
    },
    /* 跳转页面 */
    changeView(name, id) {
      let from = this.$route.query.from
      let pId = this.$route.query.id
      let query = { pId, id, from}
      this.$router.push({ name, query })
    }
  }
}
</script>

<style lang="less">
#commentList {
  @import url('~@common/style/imgBox.less');
  .con-right-title {
    i {
      cursor: pointer;
    }
  }

  .avatar-box {
    width: 40px;
    height: 40px;

    .el-image {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }
}
</style>
